package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 配置跨域相关的配置文件
 *
 * @author Dremeue
 * @create 2020-04-11 21:34
 **/
@Configuration
public class GuliMallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        // 配置跨域
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许自定义的头部
        configuration.addAllowedHeader("*");
        // 允许访问的方法
        configuration.addAllowedMethod("*");
        // 允许跨域访问的域，可以是一个域的列表，也可以是通配符"*"。这里要注意Origin规则只对域名有效
        configuration.addAllowedOrigin("*");
        // 允许请求带有验证信息
        configuration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", configuration);

        return new CorsWebFilter(source);
    }
}
